$('#loading').show();
var id = "";
if (getUrlVars()["id"] != null) {
    id = getUrlVars()["id"];
}
else {
    id = userid;
    $("#bottom").show();
    $("#posts_field").show();
}

$.getJSON('../profile/' + id, function(data) {
    //$('#loading').show();
    if(typeof data.error != 'undefined') {
        $("#infoshort").html(data.error);
    }
    else {
        $(".name").html(data.name);
        $(".lastname").html(data.lastname);
        $(".sex").html(data.sex);
        $(".city").html(data.city);
        $(".birthdate").html(data.birthdate);
        $(".religion").html(data.religion);
        $(".country").html(data.country['name']);
        var src = $(".countryflag").attr("src").replace("img/Trollface.png", "img/flags/" + data.country['alpha_2'] + ".png");
        $(".countryflag").attr("src", src);
    //$(".datecreated").html(data.datecreated); TO BE CREATED  -  Member since: <span class="datecreated"></span><br />
    }
//$('#loading').hide();
});

$.getJSON('../connection_list/' + userid, function(data) {
    $.each(data, function(key, value) {
        if (value.user == id) {
            console.log(value.type);
            if (value.type == "pending") {
                $("#addimage").html("Request pending");
            }
            else if (value.type == "connection") {
                $("#addimage").html("Connected");
            }
        }
    });
    if (userid == id) {
        $("#addimage").html(" ");
    }
    $("#addimage").show();
});


$('#addconnection').click(function() {
    $.post('../connection_create', {
        user1: userid, 
        user2: id
    });
    $("#addimage").html("Request pending");        
});
    
messages();
function messages() {
    $('#loading').show();
    $.getJSON('../message/' + id, function(data) {
        //$("#posts").html();
        var posts = "";
        if(typeof data.error != 'undefined') {
            posts = "<b>" + data.error + "</b><br /><br />";
        }
        else {
            for (var i = 0; i < data.length; i++) {
                //console.log(data[i].id);
                posts = posts + "\
                    <div class=\"post_block\">\n\
                        <div class=\"post\">\n\
                            <div class=\"post_img\"><img src=\"img/Trollface.png\" width=\"50\" height=\"50\"/></div>\n\
                            <div class=\"post_message\">\n\
                                <a href=\"?page=profile&id=" + data[i].userid + "\"><b>" + data[i].name + " " + data[i].lastname + "</b>\n\
                                    </a><br />\n\
                                " + data[i].message + "\n\
                            </div>";
                if(userid == id) {
                    posts = posts + "\
                                <form method=\"POST\" action=\"\" name=\"delete" + i + "\">\n\
                                    <input type=\"hidden\" name=\"postid\" value=\"" + data[i].id + "\" />\n\
                                    <input type=\"submit\" name=\"submit_remove\" class=\"remove_submit\" value=\"\" \n\
                                    onclick=\"\n\
                                        var formid = " + i + ";\n\
                                        removeMessage(formid);\n\
                                        return false; \"\n\
                                    />\n\
                                </form>";
                }
                posts = posts + "\
                            <div class=\"clear\"></div>\n\
                        </div>";
                posts = posts + "<div class=\"post_reaction_block\">";
                if(data[i].reactions != "") {
                    for (var b=0;b<data[i].reactions.length;b++) {
                        posts = posts + "\
                                    <div class=\"post_reaction\">\n\
                                        <div class=\"post_reaction_img\"><img src=\"img/Trollface.png\" width=\"50\" height=\"50\"/></div>\n\
                                        <div class=\"post_reaction_message\">\n\
                                            <a href=\"?page=profile&id=" + data[i].reactions[b].userid + "\">\n\
                                                <b>" + data[i].reactions[b].name + " " + data[i].reactions[b].lastname + "</b></a><br />\n\
                                            " + data[i].reactions[b].message + "\n\
                                        </div>\n\
                                        <div class=\"clear\"></div>\n\
                                   </div>";
                    }

                }
                posts = posts + "\
                                <form method=\"POST\" action=\"\" name=\"" + i + "\" class=\"addreactionform\">\n\
                                    Post reaction:<br />\n\
                                    <textarea name=\"new_reaction\" id=\"new_reaction" + i + "\"></textarea>\n\
                                    <input type=\"hidden\" name=\"postid\" value=\"" + data[i].id + "\" id=\"postid" + i + "\" /><br />\n\
                                    <input type=\"hidden\" name=\"formid\" value=\"" + i + "\" id=\"formid" + i + "\" />\n\
                                    <input type=\"submit\" name=\"submit_reaction\" value=\"Place a reaction\" class=\"submit_reaction\" id=\"" + i + "\"\n\
                                        onclick=\"\n\
                                            var formid = " + i + ";\n\
                                            submitReaction(formid);\n\
                                            return false; \"\n\
                                        />\n\
                                </form>\n\
                            </div>";
                posts = posts + "\
                    </div>";
            }
        }
        $("#posts").html(posts);
        //console.log(data[0].reactions.length);
        $('#loading').hide();
    });
}
    
$("#submitMessage").click(function(){
    submitMessage();
    return false; 
});
    
function submitMessage() {
    $.post('../message_add/' + id, $("#addmessageform").serialize(), function(data) {
        //alert(data);
        var data = jQuery.parseJSON(data);
        if (data.addmessage == true) {
            //location.reload();
            messages();
        } else {
            $("#error").html(data.error);
        }
    });
}
    
function submitReaction(formid) {
    $.post('../message_add/' + id, $('form[name="' + formid + '"]').serialize(), function(data) {
        //alert(data);
        var data = jQuery.parseJSON(data);
        if(data.addmessage == true){
            messages();
        }else{
            $("#error").html(data.error);
        }
    });
}
    
function removeMessage(formid) {
    $.post('../message_remove/' + id, $('form[name="delete' + formid + '"]').serialize(), function(data) {
        //alert(data);
        var data = jQuery.parseJSON(data);
        if(data.removemessage == true){
            messages();
        }else{
            $("#error").html(data.error);
        }
    });
}   